<?php

	/**
	 * 提问控制器
	 */
	 class QuestController extends AuthController{
	 	
		private $model;
		
		public function __init(){
			$this->model = K('Quest');
		}
		/*
		 * 显示所有提问
		 */
		public function index(){
			//查询所有的问题
			$status = Q('get.status',0,'intval');
			$total = 0;
			if($status==1){
				//查询所有未回复的提问
				$total = $this->model->where("answer=''")->count();
				$where = "answer=''";
			}elseif($status==2){
				//查询所有已回复的提问
				$total = $this->model->where("answer!=''")->count();
				$where = "answer!=''";
			}else{
				$total = $this->model->count();
				$where = "";
			}			
			$page = new Page($total,10,10);
			$this->assign('page',$page->show(3));
			$quests = M()->field('qid,quest_content,quest_time,answer,goods_name,gid,username,uid')->join('__questanswer__ qa JOIN __goods__ g JOIN __users__ u ON qa.users_uid=u.uid AND qa.goods_gid=g.gid')->where($where)->all();
			
			$this->assign('quests',$quests);
			$this->display();
		}
		
		/**
		 * 删除一个提问
		 */
		 public function delQuest(){
		 	$qid = Q('get.qid',0,'intval');
			if($this->model->delete($qid)){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		 }
		
		/**
		 * 添加一个回复
		 */
		 public function addAnswer(){
		 	if($this->model->addAnswer()){
		 		$this->ajax(array('message'=>'回复成功','data'=>Q('post.'),'status'=>1));
		 	}else{
		 		$this->ajax(array('message'=>$this->model->error,'status'=>0));
		 	}
		 }
		
		
	 }
	